from enum import unique
from app import db
import datetime

#提取父类
class BaseModel(db.Model):
    #不生成表
    __abstract__=True
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    create_time = db.Column(db.DateTime,default=datetime.datetime.now())
    update_time = db.Column(db.DateTime,default=datetime.datetime.now())


#### 1.用户表

# 昵称、手机号、密码、头像url、会员等级、总金额
class User(BaseModel):
    __tablename__="user"
    nikename = db.Column(db.String(100),nullable=False,comment="姓名")
    mobile = db.Column(db.String(11),unique=True,comment="手机号")
    password = db.Column(db.String(200),comment="手机号")
    url = db.Column(db.String(255),comment="手机号")
    level = db.Column(db.Integer,default=1,comment="手机号")
    money = db.Column(db.Float)

# #### 2.vip记录表

# 用户id  开始时间  结束时间
class Vip(BaseModel):
    __tablename__="vip"
    userid = db.Column(db.Integer, db.ForeignKey('user.id'))
    s_time = db.Column(db.Date)
    e_time = db.Column(db.Date)

# #### 3.vip购买记录表

# 用户id、购买年限、金额、折扣、实际支付金额、支付状态
class VipRecord(BaseModel):
    __tablename__="vip_record"
    userid = db.Column(db.Integer, db.ForeignKey('user.id'))
    year = db.Column(db.Integer,default=1)
    money = db.Column(db.Float)
    discount = db.Column(db.Float)
    active_money = db.Column(db.Float)
    status = db.Column(db.Integer,default=1)

# #### 5.三方登录表
# 用户id、三方平台、token、uid
class SfLogin(BaseModel):
    __tablename__="sf_login"
    userid = db.Column(db.Integer, db.ForeignKey('user.id'))
    webfrom = db.Column(db.String(30))
    token = db.Column(db.String(100))
    uid = db.Column(db.String(100))

# #### 6.类型表

# 名称
class Ctype(BaseModel):
    __tablename__="ctype"
    name = db.Column(db.String(30))

# #### 标签表

# 名称  是否启用
class Tags(BaseModel):
    __tablename__="tags"
    status = db.Column(db.Integer,default=1)
    name = db.Column(db.String(30))
  

# #### 分类表

# 名称 、图片Url、 课程总数、描述、时长、总评价数
class CourseType(BaseModel):
    __tablename__="course_type"
    name = db.Column(db.String(30))
    url = db.Column(db.String(200))
    number = db.Column(db.Integer,default=0)
    times = db.Column(db.Integer,default=0)
    tcomment = db.Column(db.Integer,default=1)
    descrip = db.Column(db.Text)


# #### 课程表

# 名称、图片、描述、关注总数、是否发布、浏览次数、所属类型
class Course(BaseModel):
    __tablename__="course"
    name = db.Column(db.String(30))
    url = db.Column(db.String(200))
    descrip = db.Column(db.Text)
    tcount = db.Column(db.Integer,default=0)
    views = db.Column(db.Integer,default=0)
    is_publish = db.Column(db.Integer,default=1)
    ctype = db.Column(db.Integer, db.ForeignKey('course_type.id'))

# # #### 4.vip课程表
# 用户id  课程id
vipuser_course = db.Table('vipuser_course',
                       db.Column('courseid', db.Integer, db.ForeignKey('course.id'), primary_key=True),
                       db.Column('userid', db.Integer, db.ForeignKey('user.id'), primary_key=True)
                       )


# #### 课程标签表

# 课程id，标签id
course_tag = db.Table('course_tag',
                       db.Column('courseid', db.Integer, db.ForeignKey('course.id'), primary_key=True),
                       db.Column('tagid', db.Integer, db.ForeignKey('tags.id'), primary_key=True)
                       )

# #### 课程分类表
# 课程id，分类id
course_cate = db.Table('course_cate',
                       db.Column('courseid', db.Integer, db.ForeignKey('course.id'), primary_key=True),
                       db.Column('course_typeid', db.Integer, db.ForeignKey('course_type.id'), primary_key=True)
                       )

# #### 章节表

# 名称  节数  视频url  课程id 
class Jie(BaseModel):
    __tablename__="jie"
    name = db.Column(db.String(30))
    url = db.Column(db.String(200))
    tcount = db.Column(db.Integer,default=0)
    courseid = db.Column(db.Integer, db.ForeignKey('course.id'))

# #### 评论分类表

# 名称 
class CommentType(BaseModel):
    __tablename__="comment_type"
    name = db.Column(db.String(30))

# #### 评价表

# 标题 总回复数 总查看数 userid  是否最后回复（1是 0否）、 pid、是否置顶,type(1发布 2评论)
class Comment(BaseModel):
    __tablename__="comment"
    title = db.Column(db.String(30))
    tcount = db.Column(db.Integer,default=0)
    tviews = db.Column(db.Integer,default=0)
    is_last = db.Column(db.Integer,default=0)
    is_top = db.Column(db.Integer,default=0)
    userid = db.Column(db.Integer, db.ForeignKey('user.id'))
    typepid = db.Column(db.Integer, db.ForeignKey('comment_type.id'))
    pid = db.Column(db.Integer) #无限级自关联
   

# #### 训练营课程表

# 标题、介绍、图片、价格、库存
class Tcourse(BaseModel):
    __tablename__="tcourse"
    title = db.Column(db.String(30))
    url = db.Column(db.String(200))
    price = db.Column(db.Float)
    store = db.Column(db.Integer,default=999)
    intro = db.Column(db.Text)
   

# #### 购物车

# 用户id、课程id、数量
class Cart(BaseModel):
    __tablename__="cart"
    userid = db.Column(db.Integer)
    cid = db.Column(db.Integer)
    count = db.Column(db.Integer)
    is_check = db.Column(db.Integer)

# #### 订单表

# 订单号、用户id、课程id、数量、单价、总价格、状态、支付宝流水号
class Orders(BaseModel):
    __tablename__="orders"
    order_no = db.Column(db.Integer)
    userid = db.Column(db.Integer, db.ForeignKey('user.id'))
    courseid = db.Column(db.Integer)
    number = db.Column(db.Integer)
    price = db.Column(db.Float)
    tmoney = db.Column(db.Float)
    status = db.Column(db.Integer,default=1)
    trans_no = db.Column(db.String(200))

db.create_all()